package com.wasion.gydpe.dao.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.mybatis.spring.SqlSessionTemplate;

import com.wasion.gydpe.dao.GenericDao;
import com.wasion.gydpe.exception.DataAccessException;

/**
 * DAO层泛型基类，实现了基本的DAO功能
 * 
 * @author 高意
 * @param <T>
 *            实体类
 * @param <PK>
 *            主键类，必须实现Serializable接口
 * 
 */
public abstract class GenericMybatisDaoImpl<T, PK extends Serializable>
		implements GenericDao<T, PK> {

	// sqlmap.xml定义文件中对应的sqlId
	public static final String SQLID_INSERT = "insert";
	public static final String SQLID_UPDATE = "update";
	public static final String SQLID_UPDATE_PARAM = "updateParam";
	public static final String SQLID_DELETE_PARAM = "deleteParam";
	public static final String SQLID_TRUNCATE = "truncate";
	public static final String SQLID_SELECT = "select";
	public static final String SQLID_SELECT_PARAM = "selectParam";
	public static final String SQLID_COUNT = "count";
	public static final String SQLID_COUNT_PARAM = "countParam";
	public static final String SQLID_SELECT_PAGING = "selectByPaging";
	public static final String SQLID_SELECTONE_PARAM = "selectOne";

	@Resource
	protected SqlSessionTemplate session;

	protected String sqlmapNamespace = "";

	/**
	 * sqlmapNamespace，对应sqlmap.xml中的命名空间
	 * 
	 * @return
	 */
	public String getSqlmapNamespace() {
		return sqlmapNamespace;
	}

	/**
	 * sqlmapNamespace的设置方法，可以用于spring注入
	 * 
	 * @param sqlmapNamespace
	 */
	public void setSqlmapNamespace(String sqlmapNamespace) {
		this.sqlmapNamespace = sqlmapNamespace;
	}

	public int add(T entity) throws DataAccessException {
		return session.insert(sqlmapNamespace + "." + SQLID_INSERT, entity);
	}

	public boolean remove(Map<String, Object> param) throws DataAccessException {
		return session
				.delete(sqlmapNamespace + "." + SQLID_DELETE_PARAM, param) > 0;
	}

	public boolean truncate() throws DataAccessException {
		int rows = session.delete(sqlmapNamespace + "." + SQLID_TRUNCATE);
		return rows > 0 ? true : false;
	}

	public boolean modify(T entity) throws DataAccessException {
		return session.update(sqlmapNamespace + "." + SQLID_UPDATE, entity) > 0;
	}

	public boolean modify(Map<String, Object> param) throws DataAccessException {

		return session
				.update(sqlmapNamespace + "." + SQLID_UPDATE_PARAM, param) > 0;
	}

	public List<T> query() throws DataAccessException {
		return session.selectList(sqlmapNamespace + "." + SQLID_SELECT);
	}

	public long count() throws DataAccessException {
		Integer count = (Integer) session.selectOne(sqlmapNamespace + "."
				+ SQLID_COUNT);
		return count.intValue();
	}

	public List<T> query(Map<String, Object> param) throws DataAccessException {
		return session.selectList(sqlmapNamespace + "." + SQLID_SELECT_PARAM,
				param);
	}

	public long count(Map<String, Object> param) throws DataAccessException {
		long count = session.selectOne(sqlmapNamespace + "."
				+ SQLID_COUNT_PARAM, param);
		return count;
	}

	public List<T> queryByPaging(Map<String, Object> param)
			throws DataAccessException {
		return session.selectList(sqlmapNamespace + "." + SQLID_SELECT_PAGING,
				param);
	}

	public T getOne(Map<String, Object> param) throws DataAccessException {
		return session.selectOne(sqlmapNamespace + "." + SQLID_SELECTONE_PARAM,
				param);
	}
}